#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "Log.hpp"
const std::string sep = ":";
const std::string defaultpath = "./Dict.txt";
class Dict
{
private:
    bool LoadDict()
    {
        std::ifstream in(_confpath);
        if (!in.is_open())
        {
            std::cerr << "open file error" << std::endl; 
            return false;
        }
        std::string line;
        while (std::getline(in, line))
        {
            if (line.empty())
                continue;
            auto pos = line.find(sep);
            if (pos == std::string::npos)
                continue;
            std::string key = line.substr(0, pos);
            std::string value = line.substr(pos + sep.size());
            LOG(DEBUG, "load info, %s: %s\n", key.c_str(), value.c_str());
            _dict.insert(std::make_pair(key, value));
        }
        in.close();
        for(auto &e : _dict)
        {
            std::cout << e.first << " : " << e.second << std::endl;
        }
        if(_dict.empty()) std::cout << " empty test " << std::endl;
        LOG(DEBUG, "load %s success\n", _confpath.c_str());
        return true;
    }

public:
    Dict(const std::string &confpath = defaultpath) : _confpath(confpath)
    {
        bool isload = LoadDict();
        if(!isload) std::cout << "load error" << std::endl;
    }

    std::string Translate(const std::string &key, bool &ok)
    {
        ok = true;
        auto iter = _dict.find(key);
        if (iter == _dict.end())
        {
            ok = false;
            return "未找到";
        }
        else
            return iter->second;
    }

    ~Dict()
    {
    }

private:
    std::string  _confpath;
    std::unordered_map<std::string, std::string>  _dict;
};